package com.probiz.estoresa.product.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;

import com.probiz.estore.common.model.product.Brand;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.product.service.BrandManager;

public class BrandController extends GenericStoreAdminController<Brand> {
    private BrandManager brandManager = null;

    public void setBrandManager(BrandManager inMgr) {
        this.brandManager = inMgr;
    }

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(Brand entity) {
		return entity.getBrandName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = brandManager;
	}
	
	@Override
	protected Map<String,String> getSortMap(){
		Map<String,String> sortMap = new HashMap<String,String>();
		sortMap.put("2", "convert_gbk(brandName)");
		sortMap.put("3", "sortOrder");
		return sortMap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#preSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */	
	@Override
	protected void preSave(HttpServletRequest request, Brand entity, BindException errors) {
		if (entity.getBrandId()==null) {
			boolean isDuplicate = brandManager.getIsBrandExist(entity.getBrandName());
			if (isDuplicate) {
				//检查品牌是否重复
				Object[] params = { entity.getBrandName() };
				errors.rejectValue("brandName", "brand.brandName.isExist", "the brandName has exist !");
			}
		} else {
			//检查修改后的品牌是否已经存在
			if (brandManager.getIsBrandNameUseByOthers(entity.getBrandName(), entity.getBrandId())) {
				errors.rejectValue("brandName", "brand.brandName.isExist", "the brandName has exist !");
			}
		}
	}
	

}
